package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;
import com.wnhz.work.jy.view.PickerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择性别
 */
public class UserSexActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private String pickCard;
    private TextView tv_change_sex;
    private String oldSex;

    private ArrayList<String> sexList = new ArrayList<>();
    private TextView view_main_right_text;
    private View btn_main_right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_sex);

        initData();
        initView();
    }

    private void initData() {
        oldSex = getIntent().getStringExtra("oldSex");
        sexList.add("男");
        sexList.add("女");
        sexList.add("保密");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("修改性别");
        view_main_right_text = (TextView) findViewById(R.id.view_main_right_text);
        view_main_right_text.setTextColor(0xffe2bb0a);
        view_main_right_text.setVisibility(View.VISIBLE);
        view_main_right_text.setText("完成");
        btn_main_right = findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.VISIBLE);
        btn_main_right.setOnClickListener(this);
        findViewById(R.id.tv_sex_true).setVisibility(View.GONE);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_sex_true).setOnClickListener(this);

        tv_change_sex = (TextView) findViewById(R.id.tv_change_sex);
        tv_change_sex.setOnClickListener(this);
        tv_change_sex.setText(oldSex);
    }

    private void showPopupWindow(View view, final List<String> list) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_educate, null);
        PickerView picker = (PickerView) contentView.findViewById(R.id.picker_educate);
        TextView pickersure = (TextView) contentView.findViewById(R.id.picker_submit);
        TextView pickercancel = (TextView) contentView.findViewById(R.id.picker_cancel);
        final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);
        ArrayList<Object> str = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            str.add(list.get(i));
        }
        pickCard = "" + str.get(1);
        picker.setData(str);
        picker.setOnSelectListener(new PickerView.onSelectListener() {

            @Override
            public void onSelect(String text) {
                pickCard = text;
            }
        });

        pickercancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                popupWindow.dismiss();
            }
        });
        pickersure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != pickCard) {
                    tv_change_sex.setText(pickCard);
                }

                popupWindow.dismiss();
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        // 设置好参数之后再show
        // popupWindow.showAsDropDown(view);
        popupWindow.showAtLocation(findViewById(R.id.ll_user_sex), Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv_change_sex.setEnabled(true);
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //修改性别
            case R.id.tv_change_sex:
                tv_change_sex.setEnabled(false);
                showPopupWindow(tv_change_sex, sexList);
                break;

            //确定
            case R.id.btn_main_right:
                setSex();
                /*//数据是使用Intent返回
                Intent intent = new Intent();

                //把返回数据存入Intent
                intent.putExtra("newSex", pickCard);

                //设置返回数据
                UserSexActivity.this.setResult(RESULT_OK, intent);

                //关闭Activity
                UserSexActivity.this.finish();*/
                break;

        }
    }

    private static final String TAG = "UserSexActivity";
    private void setSex(){
        final String sex = tv_change_sex.getText().toString();
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("sex",sex);
        showDialog();
        XUtil.Post(ConfigUtil.MODIFY_SEX,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result +"  "+sex+MyApplication.getInstance().userBean.getUserId());
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    String info = json.optString("info");
                    Toast.makeText(getApplication(),info,Toast.LENGTH_SHORT).show();
                    if ("1".equals(re)){
                        MyApplication.getInstance().userBean.setSex(sex);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }
}
